package com.itheima.reggie.common;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@RestControllerAdvice
public class GlobalExceptionHandler {

   @ExceptionHandler
    public R exception1(SQLException e){
       //输出异常信息
//       e.printStackTrace();
       if (e.getMessage().startsWith("Duplicate entry")){
           String username = e.getMessage().split(" ")[2];
           return R.error(username+"用户名已存在");
       }
       if (e.getMessage().contains("Data too long")){
           String col = e.getMessage().split(" ")[7];
           return R.error(col+"数据长度过长");
       }
        return R.error("未知错误");
    }
    @ExceptionHandler
    public R exception2(CustomerException e){


       return R.error(e.getMessage());
    }
}
